package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guoq.constant.Configration;
import com.guoq.mapper.UserMapper;
import com.guoq.pojo.Result;
import com.guoq.pojo.User;
import com.guoq.pojo.UserExample;
import com.guoq.pojo.UserExample.Criteria;
import com.guoq.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {
	@Resource
	public UserMapper userMapper;
	
//	@Override
//	public Result login(User bean) {
//		
//		UserExample example = new UserExample();
//		Criteria criteria = example.createCriteria();
//		criteria.andStatusEqualTo((byte) Configration.NORMAL);//正常
//		criteria.andRoleEqualTo(role);
//		return false;
//	}
	
	@Override
	public User getUserByUsername(String username, byte role) {
		// TODO Auto-generated method stub
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		criteria.andRoleEqualTo(role);
		criteria.andStatusEqualTo((byte) Configration.NORMAL);//正常
		example.setOrderByClause("createtime desc");
		List<User> list = this.userMapper.selectByExample(example);
		
		User user = null;
		if(!list.isEmpty()){
			user = list.get(0);
		}
		
		return user;
	}
	
	@Override
	public User getUserById(int userId) {
		// TODO Auto-generated method stub
		return this.userMapper.selectByPrimaryKey(userId);
	}
	@Override
	public List<User> getUserList() {
		// TODO Auto-generated method stub
		UserExample example = new UserExample();
		
		List<User> list = userMapper.selectByExample(example);
		return list;
	}
	/**
	 * 每20分钟执行一次
	 */
	@Override
	public void initData() {
//		userMapper.initData();
	}
	@Override
	public List<User> selectByExample(UserExample example) {
		// TODO Auto-generated method stub
		List<User> list = userMapper.selectByExample(example);
		return list;
	}
	@Override
	public User selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}
	@Override
	public int countByExample(UserExample example) {
		// TODO Auto-generated method stub
		int count = userMapper.countByExample(example);
		return count;
	}
	
	@Override
	public Result create(User bean) throws Exception {

		Result result = this.insert(bean);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: insert
	 *<P>描述:新增 
	 *@param bean
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@Override
	public Result insert(User bean){
		
		bean.setCreatetime(new Date());
		bean.setUpdatetime(new Date());
		userMapper.insertSelective(bean);
		return Result.ok();
	}
}
